package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐条件分页查询展示
     * @param dto
     * @return
     */

    @Override
    public PageResult queryByPage(SetmealPageQueryDTO dto) {
        Page<SetmealVO> page= setmealMapper.queryByPage(dto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param dto
     */
    @Transactional
    @Override
    public void save(SetmealDTO dto) {
        //新增套餐需要一同新增套餐与菜品的关系表
       Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        //先新增套餐
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);

        Long id = setmeal.getId();
        log.info("获取到新增的套餐id是：{}",id);

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(id);
        });


        //新增套餐与菜品的关系表
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 套餐状态修改
     * @param status
     * @param id
     */
    @Override
    public void updateSattus(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            List<Integer> dishStatusList =setmealDishMapper.selectDishStatusBySetmealId(id);
            dishStatusList.forEach(dishStatus->{
                if (dishStatus == StatusConstant.DISABLE) {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }


        Setmeal setmeal= Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);


    }

    /**
     * 根据套餐id查询回显套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO queryById(Long id) {
        SetmealVO setmealVO= new SetmealVO();

        //查询套餐信息封装到返回对象
        Setmeal setmeal= setmealMapper.selectBySetmeaId(id);
        BeanUtils.copyProperties(setmeal,setmealVO);

        //查询套餐与菜品关系表封装到返回对象

        List<SetmealDish> setmealDishes= setmealDishMapper.selectBySetmeaId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐内容
     * @param dto
     */
    @Transactional  //开始事务管理
    public void update(SetmealDTO dto) {
        //1、修改套餐内容
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);

        //2、删除套餐菜品关联内容
        setmealDishMapper.delete(dto.getId());


        //3、重新插入套餐菜品关系表内容
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(dto.getId());
        });
        setmealDishMapper.insert(setmealDishes);


    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //只需要判断套餐状态
        List<Setmeal> setmeals=new ArrayList<>();
        for (int i = 0; i < ids.size(); i++) {
            Setmeal setmeal = setmealMapper.selectBySetmeaId(ids.get(i));
            if (setmeal.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        setmealMapper.delete(ids);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.selectSetmealByCategoryId(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
